using System;
using System.Collections.Generic;
using System.Threading;

//
// * Copyright (c) 2002-2010 "Neo Technology,"
// *     Network Engine for Objects in Lund AB [http://neotechnology.com]
// *
// * This file is part of Neo4j.
// * 
// * Neo4j is free software: you can redistribute it and/or modify
// * it under the terms of the GNU Affero General Public License as
// * published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU Affero General Public License for more details.
// * 
// * You should have received a copy of the GNU Affero General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// 
namespace org.neo4j.kernel
{


	using GraphDatabaseService = org.neo4j.graphdb.GraphDatabaseService;

///
/// <summary> * Standalone EmbeddedGraphDatabase with Shell enabled. </summary>
/// 
	public class StandaloneWithShell
	{
		private static Logger log = Logger.getLogger(typeof(StandaloneWithShell).Name);
		private GraphDatabaseService embeddedDb;
		private AtomicBoolean shutdownInitiated = new AtomicBoolean(false);

		private GraphDatabaseService getNeo()
		{
			return this.embeddedDb;
		}

		private void addShutdownHook()
		{
			Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { shutdown(); } });
		}

		private void initialize(Map<string, string> arguments)
		{
			string path = arguments.get("path");
			this.embeddedDb = new EmbeddedGraphDatabase(path);
			Map<string, Serializable> shellProperties = Collections.emptyMap();
			getNeo().enableRemoteShell(shellProperties);
			log.info("Neo4j started at '" + path + "'");
		}

		private void blockUntilShutdown()
		{
			try
			{
				while (shutdownInitiated.get() == false)
				{
					Thread.Sleep(100);
				}
			}
			catch (InterruptedException e)
			{
				Thread.interrupted();
			// Exit
			}
		}

		private void shutdown()
		{
			if (shutdownInitiated.compareAndSet(false, true))
			{
				log.info("Shutting down...");
				try
				{
					if (getNeo() != null)
					{
						getNeo().shutdown();
						this.embeddedDb = null;
					}
				}
				catch (System.Exception t)
				{
					log.warning("Error shutting down Neo: " + t);
				}
			}
		}

		public virtual void execute(Map<string, string> arguments)
		{
			addShutdownHook();
			initialize(arguments);
			blockUntilShutdown();
		}

		static void Main(string[] args)
		{
			Map<string, string> arguments = new Dictionary<string, string>();
			for (int i = 0; i < args.Length; i++)
			{
				string arg = args[i];
				if (arg.StartsWith("-"))
				{
					string key = arg.Substring(1);
					string @value = ++i < args.Length ? args[i] : null;
					arguments.put(key, @value);
				}
			}
			if (!arguments.containsKey("path"))
			{
				Console.WriteLine("Use -path <path> to control the neo4j store path");
				return;
			}

			new StandaloneWithShell().execute(arguments);
		}
	}

}